package com.winspread.ui.squareview.base

import android.graphics.Color
import android.graphics.drawable.Drawable
import android.graphics.drawable.GradientDrawable
import androidx.annotation.ColorInt
import com.winspread.ui.utils.SizeUtils.dp2px

/**
 *Author: Amin
 *Data: 2022/4/11 11:13
 *
 *Description:  shape xml
 *
 */
class DrawableBuilder {

    private var shape = GradientDrawable.RECTANGLE

    private var lineWidth = 0

    private var lineColor = Color.TRANSPARENT

    /**
     * 为 4 个角中的每一个指定半径。对于每个角，数组包含 2 个值[X_radius, Y_radius] 。
     * 角的顺序是左上角、右上角、右下角、左下角。此属性仅在形状为RECTANGLE类型时才有效。
     * 注意：更改此属性将影响从资源加载的可绘制对象的所有实例。建议在更改此属性之前调用mutate() 。
     * 参数：半径 - 长度 >= 8 的数组，包含每个角的 4 对 X 和 Y 半径，以像素为单位指定
     */
    private lateinit var cornerRadius: FloatArray


    /**
     * 背景颜色，默认透明
     */
    @ColorInt
    private var bkColor = Color.TRANSPARENT

    /**
     * 虚线边框每个单元的长度
     */
    private var dashWidth = 0f

    /**
     * 虚线边框每个单元之间的间距
     */
    private var dashGap = 0f

    /**
     * 背景的 shape
     *
     * @return Drawable
     */
    fun build(): Drawable {
        val bg = GradientDrawable()
        bg.shape = shape
        bg.setStroke(lineWidth, lineColor, dashWidth, dashGap)
        bg.mutate()
        bg.cornerRadii = cornerRadius
        bg.setColor(bkColor)
        return bg
    }

    /**
     * 设置shape 的形状
     * @param shape Int
     * @return DrawableBuilder
     */
    fun shape(shape: Int): DrawableBuilder {
        this.shape = shape
        return this
    }

    //==================关于线的设置========================================
    /**
     * 设置线的宽和颜色
     *
     * @param width Float
     * @param color Int
     * @return DrawableBuilder
     */
    fun line(
        width: Float = defaultLineWidth,
        color: Int = defaultLineColor,
    ): DrawableBuilder {
        return lineWidth(width).lineColor(color)
    }


    /**
     * 设置线宽
     *
     * @param lineWidth Float
     * @return DrawableBuilder
     */
    fun lineWidth(lineWidth: Float): DrawableBuilder {
        this.lineWidth = lineWidth.toInt()
        return this
    }


    /**
     * 设置 线的颜色
     *
     * @param lineColor Int
     * @return DrawableBuilder
     */
    fun lineColor(lineColor: Int): DrawableBuilder {
        this.lineColor = lineColor
        return this
    }

    /**
     * 设置 线的颜色
     *   "#98989898"
     * @param lineColor Int
     * @return DrawableBuilder
     */
    fun lineColor(lineColor: String): DrawableBuilder {
        require(lineColor[0] == '#') {
            "color values must be start with # like #000000"
        }
        return lineColor(Color.parseColor(lineColor))
    }

    //==================关于圆角的设置========================================
    /**
     * 为 4 个角中的每一个指定半径。对于每个角数组包含 2 个值[X_radius, Y_radius] 。
     * 角的顺序是左上角、右上角、右下角、左下角。
     *
     * @param cornerRadius FloatArray  默认位 2
     * @return DrawableBuilder
     */
    fun corner(cornerRadius: FloatArray = defaultCornerRadius): DrawableBuilder {
        this.cornerRadius = cornerRadius
        return this
    }


    //==================关于背景设置========================================
    /**
     * 背景颜色设置
     * @param bkColor Int
     * @return DrawableBuilder
     */
    fun fill(@ColorInt bkColor: Int): DrawableBuilder {
        this.bkColor = bkColor
        return this
    }

    /**
     * 背景颜色设置
     * "#98989898"
     * @param bkColor String
     * @return DrawableBuilder
     */
    fun fill(bkColor: String): DrawableBuilder {
        require(bkColor[0] == '#') { "color values must be start with # like #000000" }
        return fill(Color.parseColor(bkColor))
    }

    //==================关于虚线设置========================================
    /**
     * 设置虚线宽度和间距
     *
     * @return DrawableBuilder
     */
    fun dash(dashWidth: Float = defaultDashWidth.toFloat(),dashGap: Float= defaultDashGap.toFloat() ): DrawableBuilder {
        return dashWidth(defaultDashWidth.toFloat()).dashGap(
            defaultDashGap.toFloat()
        )
    }

    /**
     * 设置虚线线宽
     *
     * @param dashWidth Float
     * @return DrawableBuilder
     */
    fun dashWidth(dashWidth: Float): DrawableBuilder {
        this.dashWidth = dashWidth
        return this
    }

    /**
     * 设置虚线间距
     *
     * @param dashGap Float
     * @return DrawableBuilder
     */
    fun dashGap(dashGap: Float): DrawableBuilder {
        this.dashGap = dashGap
        return this
    }


    companion object {
        //默认线条粗细 1dp
        private const val defaultLineWidth = 1F
        private val defaultLineColor = Color.parseColor("#e9e9e9")
        private val defaultCornerRadius = floatArrayOf(
            dp2px(2F).toFloat(),
            dp2px(2F).toFloat(),
            dp2px(2F).toFloat(),
            dp2px(2F).toFloat(),
            dp2px(2F).toFloat(),
            dp2px(2F).toFloat(),
            dp2px(2F).toFloat(),
            dp2px(2F).toFloat())

        //椭圆形圆角
        private const val defaultRoundCornerRadius = 100

        //默认虚线条单位长度 6dp
        private const val defaultDashWidth = 6

        //默认虚线条之间的间距 2dp
        private const val defaultDashGap = 2
    }

}